﻿using KoolearnSample.ViewModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace KoolearnSample.Model
{
    public class JsonOper
    {
        #region Method
        /// <summary>
        /// 根据地址获取网络数据 GET
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="url">网络地址</param>
        /// <param name="action">回调函数</param>
        /// <returns>解析是否成功</returns>
        public async Task<bool> GetDataAsync<T>(string url, Action<T> action)
        {
            try
            {
                var client = new HttpClient();
                var response = await client.GetAsync(url);
                var json = await response.Content.ReadAsStringAsync();
                if (!string.IsNullOrEmpty(json))
                {
                    var result = JsonConvert.DeserializeObject<T>(json);
                    action(result);
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
        }
        /// <summary>
        /// 根据地址获取网络数据 POST
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="url">网络地址</param>
        /// <param name="paramDic">传入参数</param>
        /// <param name="action">回调函数</param>
        /// <returns></returns>
        public async Task<bool> PostDataAsync<T>(string url,Dictionary<string,string> paramDic, Action<T> action)
        {
            try
            {
                var client = new HttpClient();
                var bs = new StringBuilder();
                var content = new FormUrlEncodedContent(paramDic);
                var response = await client.PostAsync(url, content);
                var json = await response.Content.ReadAsStringAsync();
                if (!string.IsNullOrEmpty(json))
                {
                    var result = JsonConvert.DeserializeObject<T>(json);
                    action(result);
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
        }
        #endregion

        #region ResultsConverts
        /// <summary>
        /// 解析注册的返回值
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public string ConvertRegisterResult(string cid)
        {
            string resultStr = "";
            switch (cid)
            {
                case "-1":
                    resultStr = "必选项为空";
                    break;
                case "-2":
                    resultStr = "用户名包含特殊字符";
                    break;
                case "-3":
                    resultStr = "用户名已经存在";
                    break;
                case "-4":
                    resultStr = "注册失败";
                    break;
                case "-6":
                    resultStr = "输入值长度超出范围";
                    break;
                default:
                    resultStr = "注册成功，注册ID为："+cid;
                    break;
            }
            return resultStr;
        }
        /// <summary>
        /// 解析提问的返回值
        /// </summary>
        /// <param name="ask_id"></param>
        /// <returns></returns>
        public string ConvertAskResult(string ask_id)
        {
            string resultStr = "";
            switch (ask_id)
            {
                case "-1":
                    resultStr = "提问失败 ";
                    break;
                case "-2":
                    resultStr = "必选项为空";
                    break;
                case "-3":
                    resultStr = "提问人不存在";
                    break;
                case "-6":
                    resultStr = "输入值长度超出范围";
                    break;
                default:
                    resultStr = "提问成功，问题ID为：" + ask_id;
                    break;
            }
            return resultStr;
        }
        /// <summary>
        /// 解析登录的返回值
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public string ConvertGetLoginResult(string cid)
        {
            string resultStr = "";
            switch (cid)
            {
                case "-1":
                    resultStr = "用户名或密码为空";
                    break;
                case "-5":
                    resultStr = "用户名或密码不正确";
                    break;
                default:
                    resultStr = "登陆成功，注册ID为：" + cid;
                    break;
            }
            return resultStr;
        }
        #endregion
    }
}
